/*
 * @Author: yanjikun
 * @Date: 2022-05-16 17:26:25
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-05-17 17:56:04
 * @Description: file content
 */

import { roleListAll } from '@/api/system/role'
import { FormConfig } from '@/components/Form/src/types/form'
import { TableColumn } from '@/components/Table/src/types/column'
import { Badge } from 'ant-design-vue'

export const userStatusOption = [
  {
    label: '正常',
    value: 1,
  },
  {
    label: '禁用',
    value: 0,
  },
]
/**搜索表单配置项 */
export const searchFormConfig: FormConfig[] = []
/**表格配置项 */
export const tableColumns: TableColumn<API.UserListItem>[] = [
  { title: '账号', key: 'login', dataIndex: 'login' },
  {
    title: '状态',
    key: 'status',
    dataIndex: 'status',
    bodyCell: ({ record }) => {
      return (
        <Badge
          status={record.status === 1 ? 'success' : 'error'}
          text={userStatusOption.find((item) => item.value === record.status)?.label}
        ></Badge>
      )
    },
  },
  { title: '联系方式', key: 'phone', dataIndex: 'phone' },
  { title: '用户名', key: 'username', dataIndex: 'username' },
  { title: '创建时间', key: 'insertAt', dataIndex: 'insertAt' },
]
/**数据表单配置项 */
export const dataFormConfig: FormConfig<API.UserCreateParams>[] = [
  {
    label: '登录名称',
    field: 'login',
    component: 'Input',
    componentProps: {
      placeholder: '请填写登录名称',
    },
    message: '请填写登录名称',
    required: true,
  },
  {
    label: '姓名',
    field: 'username',
    component: 'Input',
    componentProps: {
      placeholder: '请填写联系方式',
    },
    message: '请填写联系方式',
    required: true,
  },
  {
    label: '联系方式',
    field: 'phone',
    component: 'Input',
    componentProps: {
      placeholder: '请填写联系方式',
      maxlength: 11,
    },
    rules: [
      {
        required: true,
        message: '请填写联系方式',
      },
      {
        pattern: /^(?:0[1-9][0-9]{1,2}-)?[2-8][0-9]{6,7}$|^1[3-9][0-9]{9}$/,
        message: '请正确填写联系方式',
      },
    ],
  },
  {
    label: '消息通知',
    field: 'msgPush',
    component: 'Switch',
    componentProps: {
      style: {
        width: 'inherit',
      },
    },
    message: '请选择消息通知',
    required: true,
  },
  {
    label: '角色',
    field: 'roles',
    component: 'ApiSelect',
    componentProps: {
      api: roleListAll,
      placeholder: '请选择角色',
      fieldNames: {
        label: 'name',
        value: 'id',
      },
      mode: 'multiple',
      maxTagCount: 2,
    },
    message: '请选择角色',
    required: true,
  },
  {
    label: '密码',
    field: 'password',
    component: 'InputPassword',
    componentProps: {
      placeholder: '请填写密码',
    },
    defaultValue: '123456',
    message: '请填写密码',
    required: true,
    hidden: ({ formModel }) => {
      return !!formModel.id
    },
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    componentProps: {
      placeholder: '请填写确认密码',
    },
    defaultValue: '123456',
    message: '请填写确认密码',
    required: true,
    hidden: ({ formModel }) => {
      return !!formModel.id
    },
  },
  {
    label: '状态',
    field: 'status',
    component: 'Select',
    componentProps: {
      options: userStatusOption,
      placeholder: '请选择状态',
    },
    message: '请选择状态',
    required: true,
  },
]
